﻿Imports System.Collections.ObjectModel

Public Class PantallaInicial

    Private frases As New ObservableCollection(Of clsFrase)
    Private leccion As New clsLeccion
    Private offset As Decimal = 0
    Private vista As System.ComponentModel.ICollectionView
    Private pathImagen As String
    Private OrtografiaRevisada As Boolean

    Private Sub PantallaInicial_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        leccion.Nivel = "Intermedio"
        Me.DataContext = frases
        frases.Add(New clsFrase)
        vista = CollectionViewSource.GetDefaultView(frases)
    End Sub


#Region "KeyDown"

    Private Sub PantallaInicial_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.PreviewKeyDown

        Try

            Select Case e.Key

                Case Input.Key.F1
                    'Scrollup
                    If offset < 100 Then
                        offset = 0
                    Else
                        offset = offset - 100
                    End If
                    ScrollImagen.ScrollToVerticalOffset(offset)

                Case Input.Key.F2
                    'Scroll down
                    offset = offset + 100
                    ScrollImagen.ScrollToVerticalOffset(offset)

                Case Input.Key.F3
                    'Scrollup
                    If offset < 50 Then
                        offset = 0
                    Else
                        offset = offset - 50
                    End If
                    ScrollImagen.ScrollToVerticalOffset(offset)

                Case Input.Key.F4
                    'Scroll down
                    offset = offset + 50
                    ScrollImagen.ScrollToVerticalOffset(offset)

                Case Input.Key.F5
                    'Previous
                    If vista.CurrentPosition > 0 Then
                        vista.MoveCurrentToPrevious()
                    End If
                    tbSpanish.Focus()
                Case Input.Key.F6
                    'Next
                    If vista.CurrentPosition < frases.Count - 1 Then
                        vista.MoveCurrentToNext()
                    Else
                        Dim p As New clsFrase
                        frases.Add(p)
                        vista.MoveCurrentToNext()
                    End If
                    tbSpanish.Focus()

                Case Input.Key.F8
                    'Lesson
                    Dim frmLeccion As New Leccion
                    frmLeccion.Inicializa(leccion, pathImagen)
                    frmLeccion.ShowDialog()

                Case Input.Key.F9
                    'Clipboard es en
                    Dim str As String = ""
                    For Each p As clsFrase In frases
                        str = str & p.Castellano & vbCrLf
                    Next
                    str = str & vbCrLf & vbCrLf & vbCrLf
                    For Each p As clsFrase In frases
                        str = str & p.Ingles & vbCrLf
                    Next

                    My.Computer.Clipboard.SetText(str)
                    OrtografiaRevisada = True

                Case Input.Key.F11
                    'Correcion
                    Dim corr As New Correccion
                    corr.Inicializa(frases)
                    corr.ShowDialog()
                    tbSpanish.Focus()

                Case Input.Key.F12
                    If Not OrtografiaRevisada Then
                        MsgBox("Falta revisar ortografia")
                    Else
                        'Save
                        GuardarXML()
                        tbSpanish.Focus()
                    End If
                    

            End Select

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

#End Region

#Region "Drag and Drop"

    Private Sub GridImagen_Drop(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs) Handles GridImagen.Drop

        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            Dim fileNames As String() = e.Data.GetData(DataFormats.FileDrop, True)
            pathImagen = fileNames(0)
            Imagen.Source = New BitmapImage(New Uri(pathImagen, UriKind.Absolute))
        End If

        offset = 0
        ScrollImagen.ScrollToVerticalOffset(offset)

        e.Handled = True
    End Sub

#End Region

#Region "Guardar XML"

    Private Sub GuardarXML()
        Try

        Catch ex As Exception

        End Try
        Dim doc As XDocument = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                               <DataSet1 xmlns="http://tempuri.org/DataSet1.xsd">
                               </DataSet1>

        If leccion.Tema = String.Empty OrElse leccion.Nivel = String.Empty _
            OrElse leccion.TipoEjercicio = String.Empty OrElse leccion.Volumen = String.Empty Then
            MsgBox("Debe rellenar la información de la lección")
        Else

            Dim elemLeccion As New XElement("Leccion")
            elemLeccion.Add(<Nivel><%= leccion.Nivel %></Nivel>)
            elemLeccion.Add(<Volumen><%= leccion.Volumen %></Volumen>)
            elemLeccion.Add(<TipoEjercicio><%= leccion.TipoEjercicio %></TipoEjercicio>)
            elemLeccion.Add(<Tema><%= leccion.Tema %></Tema>)
            elemLeccion.Add(DameXmlFrases)

            Dim elemDataset1 As XElement = doc.Descendants.First
            elemDataset1.Add(elemLeccion)

            Dim path As String = System.IO.Path.GetDirectoryName(pathImagen)
            Dim pathfitxer As String = System.IO.Path.Combine(path, leccion.TipoEjercicio & " - " & leccion.Tema & ".xml")
            doc.Save(pathfitxer)
            MsgBox("File saved: " & vbCrLf & pathfitxer)

        End If

    End Sub



    Private Function DameXmlFrases() As XElement

        Dim arr As New ArrayList

        For Each f As clsFrase In frases
            Dim Xpregunta As XElement = <Frase>
                                            <Castellano><%= f.Castellano %></Castellano>
                                            <Ingles><%= f.Ingles %></Ingles>
                                            <Comentarios><%= f.Observaciones %></Comentarios>
                                        </Frase>
            arr.Add(Xpregunta)
        Next

        Return New XElement("Frases", arr.ToArray)

    End Function

#End Region

    
End Class
